//
//  WSOrderHeaderView.m
//  FD
//
//  Created by xiaofang wang on 2018/11/26.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//

#import "WSOrderHeaderView.h"
#import "WSOrderStateView.h" //订单状态:6、7、8任意一个会显示(取消订单。退款中。退款成功）
// 1,2,3,4,5任意一个会显示(已下单、制作中、派送中、已完成)

#import "WSOrderMapView.h" //status:3 会显示,其他都不显示
#import "WSOrderEvaluateView.h" //都有
@interface WSOrderHeaderView()
/**订单状态*/
@property (nonatomic, strong) WSOrderStateView *orderStatusView;
/**2.地图view*/
@property (nonatomic, strong) WSOrderMapView *orderMapView;
/**3.评价view*/
@property (nonatomic, strong) WSOrderEvaluateView *evaluateView;


@end


@implementation WSOrderHeaderView


- (instancetype)initWithFrame:(CGRect)frame
                  orderStatus:(NSNumber *)status
                      orderId:(NSString *)orderId {

    self = [super initWithFrame:frame];
    if (self) {

        [self setupUIWithStatus:status orderId:orderId];
    }
    return self;
}


- (void)setupUIWithStatus:(NSNumber *)status orderId:(NSString *)orderId{

    
    CGFloat evaluteview_y = 0;
    
    NSInteger zhuangTai = [status integerValue];
    //一 显示订单状态view h : 100
    CGRect stateViewFrame = CGRectMake(0, 0, SCREEN_WIDTH, 100);
    if (zhuangTai==6 || zhuangTai==7 || zhuangTai==8) { //6.7.8
        self.orderStatusView = [[WSOrderStateView alloc] initWithFrame:stateViewFrame stateType:(WSOrderStateViewTypeRefund)];
    }else if(zhuangTai==1 || zhuangTai==2 || zhuangTai==3 || zhuangTai==4 || zhuangTai==5) {
        self.orderStatusView = [[WSOrderStateView alloc] initWithFrame:stateViewFrame stateType:(WSOrderStateViewTypeWaitPay)];
    }
    
    if (self.orderStatusView) {
        [self addSubview:self.orderStatusView];
        //请求订单状态
        [self.orderStatusView requestOrderStatusWithOrderId:orderId status:status];
        
        evaluteview_y += 105;
    }
    
    
    
    
    //二  显示地图:只有 3 显示地图view h : 300 + 5(间距)
    if (zhuangTai == 3) {
        self.orderMapView = [[WSOrderMapView alloc] initWithFrame:CGRectMake(0, 100 + 5, SCREEN_WIDTH, 300)];
        [self addSubview:self.orderMapView];
        
        evaluteview_y += 305;
    }
    
    
    
    //三: 显示评价view
    self.evaluateView.frame = CGRectMake(0, evaluteview_y, SCREEN_WIDTH, 80);
    [self addSubview:self.evaluateView];
    
    //请求骑手信息：触发条件：订单配送方式为外送(delivery_type=0) 并且订单状态不等于0、1、2
    NSArray *statusArr = @[@(0),@(1),@(2)];
    if (![statusArr containsObject:status]) {
        [self.evaluateView requestRiderMessageWithOrderId:orderId];
    }
}






#pragma mark - 接口
//默认显示店铺地址
- (void)showDefaultShopLocationLng:(NSNumber *)lng lat:(NSNumber *)lat name:(NSString *)name {
    
    [self.orderMapView shopLocationLng:lng lat:lat name:name];
}
- (void)showReceiveAddressMarkerWithLng:(NSNumber *)lng lat:(NSNumber *)lat name:(NSString *)name {
    [self.orderMapView showReceiveAddressMarkerWithLng:lng lat:lat detailAddress:name];
}; //2019-2-18
    


//建立websocket连接
- (void)estimateWebSocketConnectionWithOrderId:(NSString *)orderId {
    [self.orderMapView wsorderMapViewEstimateWebSocketConnectionWithOrderId:orderId];
}

/**关闭websocket连接*/
- (void)closeWebSocket {
    [self.orderMapView wsOrderMapViewCloseWebSocket];
}

#pragma mark - getter
//有些东西是没法用懒加载的。那是接口写的有问题。

//- (WSOrderStateView *)orderStatusView {
//    if (!_orderStatusView) {
//        _orderStatusView = [[WSOrderStateView alloc] initWithFrame:<#(CGRect)#> stateType:<#(WSOrderStateViewType)#>];
//    }
//}

- (WSOrderEvaluateView *)evaluateView {
    if (!_evaluateView) {
        _evaluateView = [[WSOrderEvaluateView alloc] initWithFrame:CGRectZero];
    }
    return _evaluateView;
}

@end
